package oa.hleast.xswl.controller.app.place;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import oa.hleast.common.enums.QueryModeEnum;
import oa.hleast.common.result.Result;
import oa.hleast.common.result.ResultCode;
import oa.hleast.common.web.exception.BizException;
import oa.hleast.common.web.util.RequestUtils;
import oa.hleast.xswl.common.annotation.UserAuth;
import oa.hleast.xswl.pojo.dto.UmsUserDto;
import oa.hleast.xswl.pojo.form.place.CmsPlaceInfoCommentForm;
import oa.hleast.xswl.pojo.vo.place.CmsPlaceInfoCommentVo;
import oa.hleast.xswl.pojo.vo.place.CmsPlaceInfoVo;
import oa.hleast.xswl.pojo.vo.place.ShowPlaceInfoVo;
import oa.hleast.xswl.pojo.vo.place.ShowPlaceTypeVo;
import oa.hleast.xswl.service.ISysDictItemService;
import oa.hleast.xswl.service.ISysDictService;
import oa.hleast.xswl.service.IUmsUserService;
import oa.hleast.xswl.service.place.IPlaceInfoService;
import oa.hleast.xswl.service.place.IPlaceTypeService;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @author 302
 * @description
 * @date 2021/3/18
 * 2021/0/22 添加地图分类列表 & 场所信息详情 接口
 */
@Api(tags = "【移动端】地图数据")
@RestController
@RequestMapping("/api.app/v1/place-type")
@AllArgsConstructor
@Slf4j
public class PlaceTypeController {

    private final IPlaceTypeService placeTypeService;
    private final IPlaceInfoService placeInfoService;
    private final ISysDictService dictService;
    private final ISysDictItemService dictItemService;
    private final IUmsUserService umsUserService;

    @ApiOperation(value = "地图分类列表", httpMethod = "GET")
    @ApiImplicitParam(name = "id", value = "场所类型id", required = true, paramType = "query", dataType = "Long",dataTypeClass = Long.class)
    @GetMapping("getShowPlaceTypeList")
    public Result<List<ShowPlaceTypeVo>> getShowPlaceTypeList(@RequestParam @NotNull Long id) {
        List<ShowPlaceTypeVo> showPlaceTypeVoList = placeTypeService.getShowPlaceTypeList(id);

        return Result.success(showPlaceTypeVoList);
    }

    @ApiOperation(value = "场所信息列表-缓存", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "placeTypeId", value = "场所类型id", required = true, paramType = "query", dataType = "Long",dataTypeClass = Long.class),
            @ApiImplicitParam(name = "name", value = "场所名称", required = false, paramType = "query", dataType = "String",dataTypeClass = String.class),
            @ApiImplicitParam(name = "hasTicket", value = "场所名称", required = false, paramType = "query", dataType = "Integer",dataTypeClass = Integer.class),
    })
    @GetMapping("findShowPlaceInfo")
    public Result<List<ShowPlaceInfoVo>> findShowPlaceInfo(@RequestParam @NotNull Long placeTypeId, String name, @RequestParam(required = false) Integer hasTicket) {
        List<ShowPlaceInfoVo> data = placeInfoService.findShowPlaceInfo(placeTypeId, name, hasTicket);
        return Result.success(data);
    }

    @ApiOperation(value = "通过配置项key获取场所信息列表", httpMethod = "GET")
    @ApiImplicitParam(name = "configKey", value = "配置项key", required = false, paramType = "query", dataType = "String", dataTypeClass = String.class)
    @GetMapping("findShowPlaceInfoByKey")
    public Result<List<ShowPlaceInfoVo>> findShowPlaceInfoByKey(@RequestParam @NotNull String configKey) {
        List<ShowPlaceInfoVo> data = placeInfoService.findShowPlaceInfoByKey(configKey);
        return Result.success(data);
    }


    @ApiOperation(value = "场所信息详情", httpMethod = "GET")
    @GetMapping("getShowDetails")
    public Result<CmsPlaceInfoVo> getShowDetails(@RequestParam @NotNull Long id) {
        Long userId = RequestUtils.getUserId();
        CmsPlaceInfoVo placeInfoVo = placeInfoService.getShowDetails(id, userId);
        return Result.success(placeInfoVo);
    }

    @ApiOperation(value = "评论场所 -带token", httpMethod = "POST")
    @ApiImplicitParam(name = "form", value = "评论表单", required = true, paramType = "body", dataType = "CmsPlaceInfoCommentForm",dataTypeClass = CmsPlaceInfoCommentForm.class)
    @PostMapping("/commentScenicSpot")
    public Result<Boolean> commentScenicSpot(@RequestBody @NotNull CmsPlaceInfoCommentForm form) {
        Long userId = RequestUtils.getUserId();
        UmsUserDto user = umsUserService.getUserDtoById(userId);
        if (user == null) {
            return Result.failed(ResultCode.USER_NOT_EXIST);
        }

        boolean result = placeInfoService.commentScenicSpot(form, userId);
        return Result.judge(result);
    }

    @ApiOperation(value = "评论列表", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "queryMode", value = "查询模式 list 分页查询", required = true, paramType = "query", dataType = "QueryModeEnum",dataTypeClass = QueryModeEnum.class),
            @ApiImplicitParam(name = "placeInfoId", value = "场所的ID", required = true, paramType = "query", dataType = "Long",dataTypeClass = Long.class)
    })
    @GetMapping("/commentList")
    public Result<List<CmsPlaceInfoCommentVo>> commentList(@RequestParam @NotBlank String queryMode,
                                                           @RequestParam @NotNull Long placeInfoId) {
        Long userId = RequestUtils.getUserId();
        QueryModeEnum queryModeEnum = QueryModeEnum.getValue(queryMode);
        switch (queryModeEnum) {
            case LIST:
                List<CmsPlaceInfoCommentVo> result = placeInfoService.findCommentList(placeInfoId, userId);
                return Result.success(result);
            default:
                throw new BizException("查询模式错误，请联系管理员");
        }
    }

    @UserAuth
    @ApiOperation(value = "撤回评论", httpMethod = "GET")
    @ApiImplicitParam(name = "commentId", value = "场所的ID", required = true, paramType = "query", dataType = "Long",dataTypeClass = Long.class)
    @GetMapping("/cloneComment")
    public Result<Boolean> cloneComment(@RequestParam @NotNull Long commentId) {
        Long userId = RequestUtils.getUserId();
        if (userId == null) {
            return Result.failed(ResultCode.USER_NOT_EXIST);
        }
        boolean result = placeInfoService.cloneComment(commentId, userId);

        return Result.success(result);
    }


    @ApiOperation(value = "判断是否存在某条数据 -供其他服务调用", httpMethod = "GET")
    @ApiImplicitParam(name = "id", value = "id", paramType = "query", dataType = "Long",dataTypeClass = Long.class)
    @GetMapping("/countPlaceById")
    public Result<Integer> countById(@RequestParam @NotNull Long id) {
        Integer count = placeInfoService.countById(id);
        return Result.success(count);
    }

    @ApiOperation(value = "场所详情 -供其他服务调用", httpMethod = "GET")
    @ApiImplicitParam(name = "id", value = "资讯id", required = true, paramType = "path", dataType = "Long",dataTypeClass = Long.class)
    @GetMapping("get-details/{id}")
    public Result<CmsPlaceInfoVo> detail(@PathVariable Long id) {
        CmsPlaceInfoVo advert = placeInfoService.detail(id);
        return Result.success(advert);
    }
}
